package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> getDishByCategoryId(Long categoryId){
        log.info("根据分类id查询菜品：{}",categoryId);
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        List<Dish> dishes = dishService.getDishByCategoryId(dish);
        return Result.success(dishes);
    }

    /**
     * 保存菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("保存菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("菜品数据：{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);

        //清理缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        redisTemplate.delete(key);

        return Result.success();
    }

    /**
     * 菜品分查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分查询条件：{}",dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){  //String ids
        log.info("批量删除菜品：{}",ids);
        dishService.deleteBatch(ids);

        //清理缓存数据,全部清理
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 根据id查询菜品,及相应的口味
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品,及相应的口味")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品：{}",id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 更新菜品起售状态
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("更新菜品起售状态")
    @PostMapping("status/{status}")
    public Result updateStatus(@PathVariable Integer status,Long id){
        log.info("更新菜品起售状态：{}，菜品id：{}",status,id);

        Dish dish = Dish.builder()
                .id(id)
                .status(status).build();

        dishService.updateStatus(dish);

        //清理缓存数据,全部清理
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 更新菜品信息
     * @param dishDTO
     * @return
     */
    @ApiOperation("更新菜品信息")
    @PutMapping()
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("更新菜品信息：{}",dishDTO);
        dishService.updateDish(dishDTO);

        //清理缓存数据,全部清理,可能会影响多个分类
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 统一清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
